import { Inject, Module, OnModuleInit } from '@nestjs/common';
import { CACHE_MANAGER, CacheModule } from '@nestjs/cache-manager';
import { Cache } from 'cache-manager';
// 静态资源
import { ServeStaticModule } from '@nestjs/serve-static';
import { ScheduleModule } from '@nestjs/schedule';
// kernel modules
import { KernelModule } from '../kernel/kernel.module';
// import { ConfigModule } from '@nestjs/config';
import { TypeOrmModule } from '@nestjs/typeorm';
import { AppController } from './app.controller';
import { AppService } from './app.service';
import { AuthorizeModule } from './authorize/authorize.module';
import { UsersModule } from './users/users.module';
import { RolesModule } from './roles/roles.module';
import { PermissionsModule } from './permissions/permissions.module';
import { MenusModule } from './menus/menus.module';
import { UserRolesModule } from './user-roles/user-roles.module';
import { RoleMenusModule } from './role-menus/role-menus.module';
import { RolePermissionsModule } from './role-permissions/role-permissions.module';
import { OrganizationsModule } from './organizations/organizations.module';
import { DepartmentsModule } from './departments/departments.module';
import { TokensModule } from './tokens/tokens.module';
import { TokensService } from './tokens/tokens.service';
import { Token } from './tokens/entities/token.entity';
import { EmailService } from './email/email.service';
import { EmailModule } from './email/email.module';
import { join } from 'path';

console.info(__dirname);
console.info(join(__dirname, '..', '..', 'static'));

@Module({
  imports: [
    // 静态资源
    ServeStaticModule.forRoot({
      rootPath: join(__dirname, '..', '..', 'static'),
    }),
    // 开启定时任务
    ScheduleModule.forRoot(),
    // ConfigModule.forRoot(),
    TypeOrmModule.forRoot({
      type: 'mysql',
      // host: 'localhost',
      host: '0.0.0.0',
      port: 3306,
      // username: 'root',
      // password: 'Zhou199268$...',
      username: 'herry',
      password: '123456',
      database: 'orange',
      entities: [__dirname + '/**/*.entity{.ts,.js}'],
      // 设置 synchronize: true 不应在生产中使用 - 否则你可能会丢失生产数据。
      synchronize: true,
    }),
    TypeOrmModule.forFeature([Token]),
    /**
     * 启用缓存
     * @description @nestjs/cache-manager不支持cache-manager V6+
     * 2个月以前发布的v6版本 需手动降到v5版本
     */
    CacheModule.register({
      ttl: 60 * 1000, // 5 seconds
      max: 10, // maximum number of items in cache
      isGlobal: true,
    }),
    KernelModule,
    AuthorizeModule,
    UsersModule,
    RolesModule,
    PermissionsModule,
    MenusModule,
    UserRolesModule,
    RoleMenusModule,
    RolePermissionsModule,
    OrganizationsModule,
    DepartmentsModule,
    TokensModule,
    EmailModule,
  ],
  controllers: [AppController],
  providers: [AppService, TokensService, EmailService],
})
export class AppModule implements OnModuleInit {
  constructor(
    @Inject(CACHE_MANAGER) private cacheManager: Cache,
    private tokensService: TokensService,
  ) {}
  async onModuleInit() {
    console.info('onModuleInit');
    const tokens = await this.tokensService.findAll();
    // 缓存token到内存
    tokens.forEach((token) => {
      console.info(`TOKEN-${token.userId}`, token.token);
      this.cacheManager.set(`TOKEN-${token.userId}`, token.token);
    });
  }
}
